package com.example.application;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.example.application.setting.HelpActivity;
import com.example.application.cities.Search;
import com.example.application.task.WeatherTask1_2;
import com.example.application.weather.OrderPagerAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private String TAG = "MainActivity";
    Handler handler;
    List<HashMap<String, String>> list;
    String dress1, dress2, wash1, wash2, cold1, cold2, sport1, sport2, ray1, ray2, guomin1, guomin2,
            umbrella1, umbrella2, fish1, fish2, number1, number2;
    TextView  versionTv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager2 viewPager2 = findViewById(R.id.viewpager2);
        viewPager2.setAdapter(new OrderPagerAdapter(this));
        viewPager2.setUserInputEnabled(false);
        TabLayout tabLayout = findViewById(R.id.tabLayout);
        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
                tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                switch (position) {
                    case 0: {
                        tab.setText("查看");
                        tab.setIcon(R.drawable.homepage);
                        break;
                    }
                    case 1: {
                        tab.setText("资讯");
                        tab.setIcon(R.drawable.edit);
                        break;
                    }
                    case 2: {
                        tab.setText("更多");
                        tab.setIcon(R.drawable.more);
                        break;
                    }
                    default: {
                        tab.setText("我的");
                        tab.setIcon(R.drawable.mine);
                    }
                }
            }
        }
        );
        tabLayoutMediator.attach();
        handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 5) {
                    list = (List<HashMap<String, String>>) msg.obj;
                    Log.i(TAG, "handleMessage: " + list);
                    dress1 = list.get(1).get("type");
                    dress2 = list.get(1).get("tip");
                    guomin1 = list.get(0).get("type");
                    guomin2 = list.get(0).get("tip");
                    fish1 = list.get(2).get("type");
                    fish2 = list.get(2).get("tip");
                    cold1 = list.get(3).get("type");
                    cold2 = list.get(3).get("tip");
                    ray1 = list.get(4).get("type");
                    ray2 = list.get(4).get("tip");
                    wash1 = list.get(5).get("type");
                    wash2 = list.get(5).get("tip");
                    sport1 = list.get(6).get("type");
                    sport2 = list.get(6).get("tip");
                    umbrella1 = list.get(7).get("type");
                    umbrella2 = list.get(7).get("tip");
                    number1 = list.get(8).get("type");
                    number2 = list.get(8).get("tip");
                }
                super.handleMessage(msg);
            }
        };
        WeatherTask1_2 mytask = new WeatherTask1_2();
        mytask.setHandler(handler);
        Thread t = new Thread(mytask);
        t.start();
    }

    public void click(View tw) {
        Log.i(TAG, "click:");
        if (tw.getId()==R.id.more){
            Intent intent=new Intent(this, Search.class);
            startActivity(intent);
        }
        if (dress1 == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示")
                    .setIcon(R.drawable.load)
                    .setMessage("正在加载...")
                    .setPositiveButton("确定", null);
            builder.create().show();
        } else {
            if (tw.getId() == R.id.frag_index_tv_dress) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("穿衣指数")
                        .setIcon(R.drawable.icon_cloth)
                        .setMessage(dress1 + "\n" + dress2)
                        .setPositiveButton("确定", null);
                builder.create().show();
            }
            if (tw.getId() == R.id.frag_index_tv_washcar) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("洗车指数")
                        .setIcon(R.drawable.icon_car)
                        .setMessage(wash1 + "\n" + wash2)
                        .setPositiveButton("确定", null);
                builder.create().show();
            }
            if (tw.getId() == R.id.frag_index_tv_cold) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("感冒指数")
                        .setIcon(R.drawable.icon_ganmao)
                        .setMessage(cold1 + "\n" + cold2)
                        .setPositiveButton("确定", null);//注意不是positivebutton
                builder.create().show();
            }
            if (tw.getId() == R.id.frag_index_tv_sport) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("运动指数")
                        .setIcon(R.drawable.icon_sport)
                        .setMessage(sport1 + "\n" + sport2)
                        .setPositiveButton("确定", null);//注意不是positivebutton
                builder.create().show();
            }
            if (tw.getId() == R.id.frag_index_tv_rays) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("紫外线指数")
                        .setIcon(R.drawable.icon_light)
                        .setMessage(ray1 + "\n" + ray2)
                        .setPositiveButton("确定", null);//注意不是positivebutton
                builder.create().show();
            }
            if (tw.getId() == R.id.frag_index_tv_xianxing) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("限行尾号")
                        .setIcon(R.drawable.icon_car_)
                        .setMessage(number1 + "\n" + number2)
                        .setPositiveButton("确定", null);//注意不是positivebutton
                builder.create().show();
            }
            if (tw.getId() == R.id.frag_index_tv_umbrella) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("雨伞指数")
                        .setIcon(R.drawable.icon_umbrella)
                        .setMessage(umbrella1 + "\n" + umbrella2)
                        .setPositiveButton("确定", null);//注意不是positivebutton
                builder.create().show();
            }
            if (tw.getId() == R.id.frag_index_tv_fish) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("钓鱼指数")
                        .setIcon(R.drawable.icon_fish)
                        .setMessage(fish1 + "\n" + fish2)
                        .setPositiveButton("确定", null);//注意不是positivebutton
                builder.create().show();
            }
            if (tw.getId() == R.id.frag_index_tv_guomin) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("过敏指数")
                        .setIcon(R.drawable.icon_guomin)
                        .setMessage(guomin1 + "\n" + guomin2)
                        .setPositiveButton("确定", null);//注意不是positivebutton
                builder.create().show();
            }
        }
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.more_tv_help:
                Intent intent = new Intent(this, HelpActivity.class);
                startActivity(intent);
                break;
            case R.id.more_tv_share:
                shareSoftwareMsg("观天气app，快来下载吧！");
                break;
            case R.id.more_tv_version:
                versionTv=findViewById(R.id.more_tv_version);
                String versionName = getVersionName();
                versionTv.setText("当前版本:    v"+versionName);
                break;
        }
    }
    public String getVersionName() {
        PackageManager manager = getPackageManager();
        String versionName = null;
        try {
            PackageInfo info = manager.getPackageInfo(getPackageName(), 0);
            versionName = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            Log.i(TAG, "getVersionName: notfound");
        }
        return versionName;
    }
    private void shareSoftwareMsg(String s) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT,s);
        startActivity(Intent.createChooser(intent,"观天气"));
    }
}

